package com.cx.reactor.study;

import com.cx.common.utils.StringUtils;
import reactor.core.publisher.Flux;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @Author:cxy
 * @Date:2022/3/4 20:50
 */
public class FluxTest {

    private static String name;

    public static void main(String[] args) {
//        final Flux<String> just = Flux.just("1", "2");
//        just.doOnNext(System.out::println)
//                .doOnComplete(()-> System.out.println(":"))
//                .subscribe();

        getList().switchIfEmpty(s-> System.out.println("如果空的"))
                .take(1)
                .doOnError(a-> StringUtils.equals(String.valueOf(a),"!"),(throwable)-> {throw new RuntimeException(throwable); })
                .doOnComplete(()-> System.out.println("完成"))
                .subscribe();



    }




    public static Flux<?> getList(){
        return Flux.just("1","2");
    }



    private  static <T> String getFun(Function<T, String> function,T t) {

        // 消费者 输入一个 值 没有返回
        final Consumer<Void> consumer  = System.out::println;

        // 创建者  无输入 返回一个值
        final Supplier<Object> supplier =  FluxTest::getName;

        // 转换者  输入一个A 出来一个B
//        final Function<Integer,String> function = (A) -> "B";

        // 判断者  输入一个表达式 返回true or false
        final Predicate<Integer> predicate = (a) -> a.equals(1);

        final Function<TestObj, String> getName = TestObj::getName;
        return function.apply(t);
    }

    private  static <T extends TestObj> Function<T, Object> getFun1(T t) {
        return T::getName;
    }

    public static String getName() {
        return name;
    }
}
